#!/usr/bin/perl -w

use Test::More tests => 47;
use Test::Exception;
use Cwd qw/abs_path/;
use blib;
use File::Temp qw/tempdir/;
use strict;
use Data::Dumper;

BEGIN {
use_ok( 'WebPAC::Store' );
}

ok(my $abs_path = abs_path($0), "abs_path");
$abs_path =~ s#/[^/]*$#/#;
diag "abs_path: $abs_path";

my $db;
my $debug = shift @ARGV;
my $no_log = 1;	# force no log output
$no_log = 0 if ($debug);

diag "NULL Store";

throws_ok { new WebPAC::Store() } qr/path/, 'new without path';

ok(new WebPAC::Store( path => '/tmp' ), 'new without database');

ok($db = new WebPAC::Store( path => '/tmp', database => 'foobar', debug => $debug ), "new");

cmp_ok($db->path, 'eq', '/tmp', "path");

throws_ok { $db->load_ds() } qr/id/, 'load_ds without id';
ok(! $db->load_ds( id => 000 ), 'load_ds');

throws_ok { $db->save_ds() } qr/id/, "save_ds without id";
throws_ok { $db->save_ds( id => 000 ) } qr/ds/, 'save_ds without ds';

undef $db;

ok(my $path = tempdir( CLEANUP => $debug ? 0 : 1 ), "path");

diag "Store path: $path";

ok($db = new WebPAC::Store( path => $path, database => 'webpac-test', debug => $debug, no_log => $no_log ), "new");

cmp_ok($db->{'path'}, 'eq', $path, "path");
cmp_ok($db->path, 'eq', $path, "path");

ok(! $db->path(''), "path - disable caching");

ok(! defined($db->{'path'}), "no path");

ok($db->path( $path ), "path($path)");

cmp_ok($db->{'path'}, 'eq', $path, "path");

#
# test *_ds
#

throws_ok { $db->load_ds() } qr/without id/, 'load_ds without arguments';
ok(! $db->load_ds( id => 000 ), 'load_ds');

throws_ok { $db->save_ds() } qr/id/, "save_ds - need id";

my $ds = {
	'Source' => {
		'name' => 'Izvor: ',
		'tag' => 'Source',
		'display' => [ 'foo' ]
	},
	'ID' => {
		'name' => 'ID',
		'tag' => 'IDths',
		'swish' => [ 'bar' ],
		'lookup_key' => [ 'bar' ]
	},
};

throws_ok { $db->save_ds( id => 1 ) } qr/ds/, "save_ds - need ds";

ok($db->save_ds( id => 1, ds => $ds ), "save_ds");

ok(my $ds2 = $db->load_ds( id => 1 ), "load_ds with id");

is_deeply($ds, $ds2, "loaded data");

throws_ok { $ds2 = $db->load_ds( 1 ) } qr/HASH/, "load_ds without hash";

ok($ds2 = $db->load_ds( id => 1 ), "load_ds");

is_deeply($ds, $ds2, "loaded data");

ok(! $db->load_ds( id => 42 ), "load_ds non-existing");

ok($db = new WebPAC::Store( path => $path, database => 'webpac-test', debug => $debug, no_log => $no_log ), "new");

ok(! $db->load_ds( id => 1, input => 'foobar' ), "load_ds with invalid input");

ok(! $db->load_ds( id => 1, database => 'non-existant', ), "load_ds with unknown database");

ok($ds2 = $db->load_ds( id => 1, database => 'webpac-test' ), "load_ds");

#
# test *_lookup
#

my $l = {
	foo => { 42 => 1 },
};

ok(! $db->load_lookup( input => 'non-existant', key => 'foo' ), 'invalid load_lookup');

ok($db->save_lookup( input => 'foo', key => 'bar', data => $l ), "save_lookup");

ok(-e $db->path . '/lookup/webpac-test/foo/bar', "exists");

is_deeply($db->load_lookup( input => 'foo', key => 'bar' ), $l, 'load_lookup');

ok($db->save_lookup( database => 'baz', input => 'foo', key => 'bar', data => $l ), "save_lookup with database");

ok(-e $db->path . '/lookup/baz/foo/bar', "exists");

is_deeply($db->load_lookup( database => 'baz', input => 'foo', key => 'bar' ), $l, 'load_lookup');

#
# test *_row
#

my $row = {
	'000' => [ 42 ],
	'900' => [ qw/a foo b bar c baz/ ],
};

ok(! $db->load_row( input => 'non-existant', id => 1234 ), 'invalid load_row');

ok($db->save_row( input => 'foo', id => 1234, row => $row ), "save_row");

ok(-e $db->path . '/row/webpac-test/foo/1234', "exists");

is_deeply($db->load_row( input => 'foo', id => 1234 ), $row, 'load_row');

ok($db->save_row( database => 'baz', input => 'foo', id => 1234, row => $row ), "save_row with database");

ok(-e $db->path . '/row/baz/foo/1234', "exists");

is_deeply($db->load_row( database => 'baz', input => 'foo', id => 1234 ), $row, 'load_row');

undef $db;

